package com.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wms.entity.Company;
import com.wms.entity.Fees;
import com.wms.entity.Storageentry;
import com.wms.service.CompanyService;
import com.wms.service.FeesService;
import com.wms.service.StorageentryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.wms.entity.Storageexit;
import com.wms.service.StorageexitService;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2024-06-24
 */
@RestController
@RequestMapping("/storageexit")
public class StorageexitController {

    @Autowired
    private StorageexitService storageexitService;

    @Autowired
    private FeesService feesService;

    @Autowired
    private StorageentryService storageentryService;

    @Autowired
    private CompanyService companyService;

    // 获取所有出库记录
    @GetMapping("/list")
    public List<Storageexit> list() {
        return storageexitService.list();
    }

    // 根据ID获取出库记录
    @GetMapping("/get/{id}")
    public Storageexit getById(@PathVariable Integer id) {
        return storageexitService.getById(id);
    }

    // 新增出库记录
    @PostMapping("/add")
    @Transactional
    public boolean add(@RequestBody Storageexit storageexit) {

        extracted(storageexit);

        return storageexitService.save(storageexit);
    }

    private void extracted(Storageexit storageexit) {
        Company company = companyService.getById(storageexit.getCompanyId());
        BigDecimal storageFeeRate = company.getStorageFeeRate();
        Integer freeDays = company.getFreeDays(); // 免费天数
        QueryWrapper<Storageentry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", company.getCompanyId());
        queryWrapper.eq("port_id", storageexit.getPortId());
        List<Storageentry> storageentries = storageentryService.getBaseMapper().selectList(queryWrapper);
        long between = 0;
        if (!storageentries.isEmpty()) {
            Storageentry storageentry = storageentries.get(0);
            LocalDate entryDate = storageentry.getEntryDate();
            LocalDate exitDate = storageexit.getExitDate();
            between = ChronoUnit.DAYS.between(entryDate, exitDate);
        }


        Fees fees = new Fees()
                .setOrderNumber(storageexit.getOrderNumber())
                .setRate(storageFeeRate)
                .setCompanyId(storageexit.getCompanyId())
                .setPortId(storageexit.getPortId());
        fees.setExitWeightTon(BigDecimal.valueOf(storageexit.getItemCount()));
        fees.setStorageDays((int) between);
        if (between <= freeDays) {
            fees.setFee(BigDecimal.ZERO);

        } else {
            long l = between - freeDays;
            BigDecimal d = new BigDecimal(l);

            BigDecimal multiply = d.multiply(storageFeeRate);
            BigDecimal multiply1 = multiply.multiply(storageexit.getItemWeightTon());
            fees.setFee( multiply1 );
        }

        feesService.getBaseMapper().insert(fees);
    }

    // 更新出库记录
    @PutMapping("/update")
    @Transactional
    public boolean update(@RequestBody Storageexit storageexit) {
        QueryWrapper<Fees> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number", storageexit.getOrderNumber());
        feesService.getBaseMapper().delete(queryWrapper);

        extracted(storageexit);
        return storageexitService.updateById(storageexit);
    }

    // 删除出库记录
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id) {
        return storageexitService.removeById(id);
    }
}
